# Author:Zhang Yu
import socket,os,time
server = socket.socket()
server.bind(("127.0.0.1",9999))
while True:
    server.listen()
    while True:
        conn,addr = server.accept()
        print("new conn: ",addr)
        while True:
            data = conn.recv(1024)
            if not data:
                print("客户端已断开")
                break
            print("执行指令:",data)
            cmd_res = os.popen(data.decode()).read()
            if not cmd_res:
                cmd_res = "cmd has no output ..."
            conn.send(str(len(cmd_res.encode())).encode()) #先发大小给客户端
            client_ack = conn.recv(1024) #发送确认
            conn.send(cmd_res.encode())

server.close()